package interceptors;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.opensymphony.xwork2.util.ValueStack;
import entities.User;
import java.util.List;
import java.util.Map;

/**
 *
 * @author corneliugosa
 */
public class Logout implements Interceptor{

    @Override
    public void destroy() {
        System.out.println("Destroyed!");
    }

    @Override
    public void init() {
        System.out.println("Initialized!");
    }

    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
        System.out.println("baaaa");
        Map<String,Object> session = invocation.getInvocationContext().getSession();
        Map<String,Object> context = invocation.getInvocationContext().getApplication();
        List<User> userList = (List<User>)context.get("userList");
        ValueStack valueStack = invocation.getInvocationContext().getValueStack();
                
        User user = (User)session.get("user");
        
        if(session==null || session.get("logged")==null){
            return "failure";
        }else if(session.get("logged").equals("yes")){
            userList.remove(user);
            context.put("userList", userList);
            
            session.clear();
            
            invocation.getInvocationContext().setApplication(context);
            invocation.getInvocationContext().setSession(session);
            
            invocation.invoke();
        }
        
        return "failure";
    }
    
}
